<?php include "../public/header.php"?>
<?php include "../public/header_public.php"?>
<?php include "../public/header_edit.php"?>
<script src="/static/admin/js/math.js"></script>
<link rel="stylesheet" type="text/css" href="/static/admin/css/main.css">
<style>
    #type_content button{width: 80%;margin-left: 10%!important;margin-top:10px;height: 35px;line-height: 29px;}
    #type_content .active{background-color:#2272eb;color: #FFFFFF}
    .usenum{padding-left: 10px;color: #409EFF;cursor: pointer;}
    .opt_a{margin-left: 5px;cursor: pointer;color: #65b5ff;font-size: 12px;}

    #commentForm .main-container .short_table_list thead tr td{background: #fafafa;}


    .td2{width: 120px!important;}
    .td3{width: 200px!important;}
    .td4{}
    .td5{width: 80px!important;}
    .td6{width: 130px!important;}
    .td7{width: 80px!important;}
    .td8{width: 80px!important;}
    .td9{width: 120px!important;}
    .td10{width: 120px!important;}
    .td11{width: 120px!important;}
    .td12{width: 120px!important;}
    .td13{width: 80px!important;}

    #cuss tr td{font-weight: bold;}
    #cuss tr td span{margin-left: 5px}
</style>
<body class="laytp-container">
<div style="height: 45px"></div>
<div id="opt_nav">
</div>
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title" id="titss"></span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
</div>
<?php include "dao.php"?>

<div class="layui-card" id="table-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <div class="layui-col-md2" style="width: 150px!important;">
                <div id="type_content">
                    <button class="layui-btn layui-btn-primary active" key="1" type="button">人工费</button>
                    <button class="layui-btn layui-btn-primary" key="2" type="button">材料费</button>
                    <button class="layui-btn layui-btn-primary" key="3" type="button">机械费</button>
                    <button class="layui-btn layui-btn-primary" key="4" type="button">分包费</button>
                </div>
            </div>
            <div class="layui-col-md10" style="width: calc(100% - 150px)!important;">
                <div class="layui-form layui-form-pane" id="commentForm">
                    <div class="main-container" style="margin:0!important;padding: 0 10px!important;">
                        <div class="layui-form-item height_auto" style="padding: 0!important;">
                            <div class="short_table_list" key="1">
                                <div style="padding: 10px 0 5px 0;">
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                                    <button type="button" onclick="func_yin()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从其他项目引入</button>
                                    <button type="button" onclick="func_chong()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">重置</button>
                                    <button type="button" onclick="func_import()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>
                                    <button type="button" onclick="func_guan()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">管理分类</button>
                                    <button type="button" onclick="saveinfo()" class="layui-btn layui-btn-sm btn-dark marginr5">保存</button>
                                    <div class="biantit" style="color: #248bfe;font-size: 13px;float: right;margin-right: 10px"></div>
                                </div>
                                <div style="overflow: auto">
                                    <div style="max-width: 2000px;overflow: hidden">
                                        <table class="layui-table" lay-size="sm" id="short_tables">
                                            <thead class="xustab" id="cuss">
                                            <tr style="height: 35px!important">
                                                <td class="td1" style="width: 45px">序号</td>
                                                <td class="td2">
                                                    <span>类别</span>
                                                    <span class="layui-table-sort layui-inline" lay-sort="" field="basetype">
                                                    <i class="layui-edge layui-table-sort-asc" title="升序"></i>
                                                    <i class="layui-edge layui-table-sort-desc" title="降序"></i>
                                                </td>
                                                <td class="td3">
                                                    <span><text>*</text>名称</span>
                                                    <span class="layui-table-sort layui-inline" lay-sort="" field="name">
                                                    <i class="layui-edge layui-table-sort-asc" title="升序"></i>
                                                    <i class="layui-edge layui-table-sort-desc" title="降序"></i>
                                                </td>
                                                <td class="td4" ><span>特征描述</span></td>
                                                <td class="td5"><span>单位</span></td>
                                                <td class="td6"><span>含量计算式</span></td>
                                                <td class="td7"><span>含量</span></td>
                                                <td class="td8"><span>损耗率</span></td>
                                                <td class="td9"><span>不含税单价(元)</span></td>
                                                <td class="td10"><span>成本单价(元)</span></td>
                                                <td class="td11"><span>备注</span></td>
                                                <td class="td12"><span>溯源</span></td>
                                                <td class="td13"><span>在用</span></td>
                                            </tr>
                                            </thead>
                                            <tbody class="short_table_tbody" id="list_list" style="min-height: 40px!important;max-height: 600px!important;"></tbody>
                                        </table>
                                    </div>
                                </div>

                                <div class="listerror">提示：<span></span></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var param_type = getQueryVariable('type') || 1;
    $("#type_content button").removeClass('active');
    $("#type_content button[key='"+param_type+"']").addClass('active');

    // drag_column('short_tables');
    var engininfo;
    var basetypeArr;






    $(".xustab .layui-edge").click(function () {
        var field = $(this).parents('.layui-table-sort').attr('field');
        var sorts = $(this).parents('.layui-table-sort').attr('lay-sort');
        $(".xustab .layui-table-sort").attr('lay-sort', '');
        if(sorts == 'desc' || !sorts){
            $(this).parents('.layui-table-sort').attr('lay-sort', 'asc');
        }else{
            $(this).parents('.layui-table-sort').attr('lay-sort', 'desc');
        }

        var data = [], thisData;
        var key = 0;
        $('#list_list tr').each(function (i) {
            var value = ConvertPinyin($(this).find('.'+field).val() || '');
            data.push({nums:key, field:value});
            key++;
        })

        if(sorts == 'desc' || !sorts){
            thisData = layui.sort(data, 'field');
            $(this).parents('.layui-table-sort').attr('lay-sort', 'asc');
        }else{
            thisData = layui.sort(data, 'field', true);
            $(this).parents('.layui-table-sort').attr('lay-sort', 'desc');
        }

        var listObj =document.getElementById("list_list");
        var TrObj = $(listObj).children('tr');
        for (var key in thisData){
            listObj.appendChild(TrObj[thisData[key].nums]);
        }


        $('#list_list tr').each(function (i) {
            var nums = parseInt(i)+1;
            var basetype = $(this).find('td:nth-child(1)').html(nums);
        })


    });


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;

        open_mark_list();
        $("#type_content button").click(function () {
            var thisKey = $(this).attr('key');
            window.location.href = 'mark.php?close=1&enginid='+param_enginid+'&titss='+encodeURI(param_titss)+'&pagekey=mark&type='+thisKey;
        });

    });

    function saveinfo() {
        var buttype = $("#type_content .active").attr('key');
        var list = [''],repeat = [];
        try {
            $('#list_list tr').each(function (i) {
                var type = buttype;
                var basetype = $(this).find('.basetype').val();
                var name = $(this).find('.name').val();
                var listid = $(this).find('.name').attr('listid') || 0;
                var source = $(this).find('.name').attr('source') || 0;
                var guige = $(this).find('.guige').val();
                var valunit = $(this).find('.valunit').val();
                var content_exp = $(this).find('.content_exp').val();
                var content = $(this).find('.content').val();
                var loss_rate = $(this).find('.loss_rate').val();
                var no_price = $(this).find('.no_price').val();
                var price = $(this).find('.price').val();
                var explain = $(this).find('.explain').val();

                var index = i + 1;
                if(!name){
                    throw new Error('第'+index+'行，请填写名称');
                }
                var str = type+'-'+basetype+'-'+name+'-'+guige+'-'+valunit;
                if($.inArray(str, repeat) != -1){
                    throw new Error('第'+index+'行，内容重复');
                }
                repeat.push(str);

                list.push({
                    type:type,
                    basetype:basetype,
                    name:name,
                    id:listid,
                    source:source,
                    guige:guige,
                    valunit:valunit,
                    content_exp:content_exp,
                    content:content,
                    loss_rate:loss_rate,
                    no_price:no_price,
                    price:price,
                    explain:explain,
                    enginid: param_enginid,
                });
            });
        } catch(e) {
            $(".listerror").find('span').text(e.message);
            $(".listerror").show();
            return false;
        }
        $(".listerror").hide();

        facade.ajax({url: ask_main_mark_edit, data:{list:list,enginid: param_enginid,type:buttype}}).done(function(res){
            if (res.code === 2000) {
                facade.success("提交成功!");
                setTimeout(function () {
                    window.location.reload();
                },1000)
            }
        });
    }





    function open_mark_list() {
        var buttype = $("#type_content .active").attr('key');
        facade.ajax({url: ask_main_mark_list,data:{enginid:param_enginid, type:buttype}}).done(function(res){
            if (res.code === 2000) {
                $(".biantit").html(res.data.tit);

                engininfo = res.data.engininfo;
                basetypeArr = res.data.basetypearr;

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td  class="td1" style="width: 45px">1</td>';
                listAddStr += '<td  class="td2"><select class="layui-select basetype">';
                listAddStr += '<option value="">请选择</option>';
                var this_basetype = basetypeArr[buttype];
                for(var j=0;j<this_basetype.length;j++){
                    listAddStr += '<option value="'+this_basetype[j].name+'">'+this_basetype[j].name+'</option>';
                }
                listAddStr += '</select></td>';
                listAddStr += '<td  class="td3"><input type="text" class="layui-input name" listid="0" source="0"></td>';
                listAddStr += '<td  class="td4"><textarea class="layui-textarea curarea guige"></textarea></td>';
                listAddStr += '<td  class="td5"><input type="text" class="layui-input valunit"></td>';
                listAddStr += '<td  class="td6"><input type="text" class="layui-input content_exp" value="1"></td>';
                listAddStr += '<td  class="td7"><input type="number" class="layui-input content" value="1" readonly></td>';
                listAddStr += '<td  class="td8"><input type="number" class="layui-input loss_rate" value="0"></td>';
                listAddStr += '<td  class="td9"><input type="number" class="layui-input no_price">';
                listAddStr += '<td  class="td10"><input type="number" readonly class="layui-input price"></td>';
                listAddStr += '<td  class="td11"><input type="text" class="layui-input explain"></td>';
                listAddStr += '<td  class="td12">';
                listAddStr += '<a class="opt_a opt_xuan" onclick="func_opt_xuan(this)">选择</a>';
                listAddStr += '<a class="opt_a opt_ru" onclick="func_opt_ru(this)">入库</a>';
                listAddStr += '<a class="opt_a opt_cha layui-hide" onclick="func_opt_cha(this)">查看</a>';
                listAddStr += '<a class="opt_a opt_qing layui-hide" onclick="func_opt_qing(this)">清空</a>';
                listAddStr += '</td>';
                listAddStr += '<td class="td13"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);




                var editStr = '';
                var list = res.data.data;
                for (var i=0;i<list.length;i++){
                    editStr += '<tr>';
                    editStr += '<td class="td1" style="width: 45px">'+(i + 1)+'</td>';
                    editStr += '<td class="td2"><select class="layui-select basetype">';
                    editStr += '<option value="">请选择</option>';

                    var this_basetype = basetypeArr[buttype];
                    for(var j=0;j<this_basetype.length;j++){
                        if(list[i].basetype == this_basetype[j].name){
                            editStr += '<option value="'+this_basetype[j].name+'" selected>'+this_basetype[j].name+'</option>';
                        }else{
                            editStr += '<option value="'+this_basetype[j].name+'">'+this_basetype[j].name+'</option>';
                        }
                    }
                    editStr += '</select></td>';
                    editStr += '<td class="td3"><input type="text" class="layui-input name" listid="'+list[i].id+'" source="'+list[i].source+'" value="'+list[i].name+'"></td>';
                    editStr += '<td class="td4"><textarea class="layui-textarea curarea guige">'+list[i].guige+'</textarea></td>';
                    editStr += '<td class="td5"><input type="text" class="layui-input valunit" value="'+list[i].valunit+'"></td>';
                    editStr += '<td class="td6"><input type="text" class="layui-input content_exp" value="'+list[i].content_exp+'"></td>';
                    editStr += '<td class="td7"><input type="number" class="layui-input content" readonly value="'+list[i].content+'"></td>';
                    editStr += '<td class="td8"><input type="number" class="layui-input loss_rate" value="'+list[i].loss_rate+'"></td>';
                    editStr += '<td class="td9"><input type="number" class="layui-input no_price" value="'+list[i].no_price+'">';
                    editStr += '<td class="td10"><input type="number" class="layui-input price" readonly value="'+list[i].price+'"></td>';
                    editStr += '<td class="td11"><input type="text" class="layui-input explain" value="'+list[i].explain+'"></td>';
                    editStr += '<td class="td12">';
                    if(list[i].source > 0){
                        editStr += '<a class="opt_a opt_xuan" onclick="func_opt_xuan(this)">选择</a>';
                        editStr += '<a class="opt_a opt_ru layui-hide" onclick="func_opt_ru(this)">入库</a>';
                        editStr += '<a class="opt_a opt_cha" onclick="func_opt_cha(this)">查看</a>';
                        editStr += '<a class="opt_a opt_qing" onclick="func_opt_qing(this)">清空</a>';
                    }else{
                        editStr += '<a class="opt_a opt_xuan" onclick="func_opt_xuan(this)">选择</a>';
                        editStr += '<a class="opt_a opt_ru" onclick="func_opt_ru(this)">入库</a>';
                        editStr += '<a class="opt_a opt_cha layui-hide" onclick="func_opt_cha(this)">查看</a>';
                        editStr += '<a class="opt_a opt_qing layui-hide" onclick="func_opt_qing(this)">清空</a>';
                    }
                    editStr += '</td>';
                    editStr += '<td class="td13" onclick="func_cha_basal('+list[i].id+')">';
                    editStr += '<span class="usenum">'+list[i].usenum+'</span>';
                    editStr += '</td>';
                    editStr += '</tr>';
                }
                $('#list_list').html(editStr);
                update_refresh_row_func();

                render_from();

                $(".short_table_add_row").click();
                var lllen = $("#list_list tr").length;
                $("#list_list tr:nth-child("+lllen+")").remove();
            }
        });
    }
    function func_yin(){
        var buttype = $("#type_content .active").attr('key');
        openPage('mark_yin.php?page=mark&type='+buttype+'&enginid='+param_enginid, '80%','80%');
    }
    function func_chong(){
        var buttype = $("#type_content .active").attr('key');
        openPage('mark_chong.php?page=mark&type='+buttype, '80%','80%');
    }
    function func_import(){
        openPage('../public/import.php?reveal=main_engin@importdata_mark', '770px', '490px');
    }
    function func_guan(){
        var buttype = $("#type_content .active").attr('key');
        openPage('mark_guan.php?close=1&enginid='+param_enginid+'&type='+buttype, '80%','80%');
    }
    function func_cha_basal(markid){
        openPage('mark_basal.php?markid='+markid+'&enginid='+param_enginid, '80%','80%');
    }
    function func_opt_xuan(obj) {
        var buttype = $("#type_content .active").attr('key');
        var domkey = $(obj).parents('tr').index();
        if(buttype == 1){
            openPage('check_main_base_ren.php?page=mark&domkey='+domkey, '80%','80%');
        }
        if(buttype == 2){
            openPage('check_main_base_cai.php?page=mark&domkey='+domkey, '80%','80%');
        }
        if(buttype == 3){
            openPage('check_main_base_ji.php?page=mark&domkey='+domkey, '80%','80%');
        }
        if(buttype == 4){
            openPage('check_main_base_fen.php?page=mark&domkey='+domkey, '80%','80%');
        }
    }
    function func_opt_ru(obj) {
        var isku = 1;
        var ku_index = $(obj).parents('tr').index();
        var buttype = $("#type_content .active").attr('key');
        if(buttype == 1){
            openPage('../main_base_ren/add.php?close=1&isku='+isku+'&ku_index='+ku_index, '80%','80%');
        }
        if(buttype == 2){
            openPage('../main_base_cai/add.php?close=1&isku='+isku+'&ku_index='+ku_index, '80%','80%');
        }
        if(buttype == 3){
            openPage('../main_base_ji/add.php?close=1&isku='+isku+'&ku_index='+ku_index, '80%','80%');
        }
        if(buttype == 4){
            openPage('../main_base_fen/add.php?close=1&isku='+isku+'&ku_index='+ku_index, '80%','80%');
        }
    }
    function func_opt_cha(obj) {
        var buttype = $("#type_content .active").attr('key');
        var source = $(obj).parents('tr').find('.name').attr('source') || 0;
        if(buttype == 1){
            openPage('../main_base_ren/show.php?id='+source, '80%','80%');
        }
        if(buttype == 2){
            openPage('../main_base_cai/show.php?id='+source, '80%','80%');
        }
        if(buttype == 3){
            openPage('../main_base_ji/show.php?id='+source, '80%','80%');
        }
        if(buttype == 4){
            openPage('../main_base_fen/show.php?id='+source, '80%','80%');
        }
    }
    function func_opt_qing(obj) {
        var source = $(obj).parents('tr').find('.name').attr('source', 0);
        $(obj).parents('tr').find('.opt_ru').removeClass('layui-hide');
        $(obj).parents('tr').find('.opt_cha').addClass('layui-hide');
        $(obj).parents('tr').find('.opt_qing').addClass('layui-hide');
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        ji_suan_list();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });


            var divs = document.querySelectorAll(".curarea");
            for (let i = 0; i < divs.length; ++i) {
                var deaHeight = divs[i].scrollHeight
                deaHeight = deaHeight < 35 ? 35 : deaHeight
                if(deaHeight == 35){
                    $(divs[i]).attr('style','height:'+deaHeight+'px!important;line-height:35px!important')
                }else{
                    $(divs[i]).attr('style','height:'+deaHeight+'px!important')
                }

                divs[i].addEventListener('input', (e) => {
                    var shiHei = parseInt(e.target.scrollHeight);
                    shiHei = shiHei < 35 ? 35 : shiHei
                    if(shiHei == 35){
                        $(divs[i]).attr('style','height:'+shiHei+'px!important;line-height:35px!important')
                    }else{
                        $(divs[i]).attr('style','height:'+shiHei+'px!important')
                    }
                });
            }



            setTimeout(function () {
                $(".xustab").attr('style','overflow-y: hidden;');
            },100);

        },100);
    }

    $('#list_list').on('input propertychange','.content_exp,.loss_rate,.no_price',function () {
        ji_suan_list();
    });
    function ji_suan_list(){
        $('#list_list tr').each(function () {
            var content_exp = $(this).find('.content_exp').val() || 0;
            var content = mathStrNumber(content_exp, 6);
            $(this).find('.content').val(content);

            var loss_rate = parseFloat($(this).find('.loss_rate').val()) || 0;
            var no_price = parseFloat($(this).find('.no_price').val()) || 0;

            var price = mathRound (no_price * (1 + loss_rate) * content, 3);
            $(this).find('.price').val(price);
        });
    }
</script>
</body>
</html>
